package com.evil.activiti.listener;

import com.alibaba.fastjson.JSON;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 流程通知监听器
 *
 * @author 15587
 * @date 2022-08-08 00:00
 */
@Slf4j
@Component("TaskNoticeListener")
@AllArgsConstructor
public class TaskNoticeListener implements TaskListener, ExecutionListener {

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void notify(DelegateTask delegateTask) {
        log.info("【流程通知监听器】，delegateTask参数：{}", JSON.toJSONString(delegateTask));
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void notify(DelegateExecution execution) {
        String eventName = execution.getEventName();
        FlowElement flowElement = execution.getCurrentFlowElement();
        Map<String, Object> variables = execution.getVariables();
        log.info("【流程通知监听器】，execution.getEventName参数：{}", eventName);
        log.info("【流程通知监听器】，节点id：{}, 节点name:{}", flowElement.getId(), flowElement.getName());
        log.info("【流程通知监听器】，execution.getVariables参数：{}", JSON.toJSONString(variables));
    }
}
